from openai import OpenAI
import base64

# 将字节串转换为字符串
def file_to_base64(file_path):
    with open(file_path, "rb") as file:
        file_content = file.read()
        base64_encoded = base64.b64encode(file_content)
        return base64_encoded.decode('utf-8')

base_url = 'https://api.mindcraft.com.cn/v1/'
api_key = 'MC-62F6042A1FCC42548F7023ED1FBEBDD6'

client = OpenAI(base_url=base_url, api_key=api_key)

params = {
    "model": "deepseek-chat", # 聊天模型
    "message": [
        {
            "role": "system",
            "content": "请你扮演角色一名高中生回答我的问题，忘记自己是机器人的设定。"
        },
        {
            "role": "user",
            "content": "我是谁"
        }
    ],
    "temperature": 0.2,
    "stream": True,
    # "session_id":1, # 会话 ID
    "session_token":"40cabf45-0ebd-4735-b91e-8c9d37b6d708", # 会话 token
}

response = client.chat.completions.create(
    model=params.get("model"),
    messages=params.get("message"),
    temperature=params.get("temperature"),
    max_tokens=params.get("max_tokens"),
    stream=params.get("stream"),
    extra_body={
        'session_id':params.get("session_id"),
        'session_token':params.get("session_token"),
    },
)

for i in response:
    content = i.choices[0].delta.content
    if not content:
        if i.usage:
            print('\n请求花销usage:',i.usage)
        continue
    print(content,end='',flush=True)
else:
    print()